package com.hyf.zookeeper.core;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

public class ZKCreate {

    private static ZooKeeper zk;

    private static ZookeeperConnection zkConn;

    public static void create(String path,byte[] data) throws KeeperException, InterruptedException {
        String s = zk.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(String.format("{'path':'%s','result':'%s'}",path,s));
    }

    public static void main(String[] args){
        String path="/MyFirstZnode2";

        byte[] data="My first zookeeper app2".getBytes();

        zkConn=new ZookeeperConnection();
        try {
            zk=zkConn.connect("47.93.157.178:12181");
            create(path,data);
            zkConn.close();
            System.out.println("end");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }
}
